//
//  SinaComposeView.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/21.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit
import SnapKit
import pop
class SinaComposeView: UIView {
    //MARK: -
    //MARK: - 懒加载控件
    private lazy var  backView:UIImageView = UIImageView(image:UIImage.snapShotCurrentWindow())
        private lazy var slogonView: UIImageView = UIImageView(image:#imageLiteral(resourceName: "compose_slogan"))
    lazy var buttonArray :[SinaComposeButton] = [SinaComposeButton]()
    //MARK: -
    //MARK: - 重写构造函数
    override init(frame:CGRect){
    
        super.init(frame: UIScreen.main.bounds)
        self.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
        setupUI()
    }
    //MARK: -
    //MARK: - 只能由代码创建
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -
    //MARK: - 搭建界面
    private func setupUI(){
        //添加被处理毛玻璃效果的背景视图  截取屏幕
        let backView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
//        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
//        toolBar.barStyle = .black
        
        self.addSubview(backView)
        //self.addSubview(toolBar)
        
        self.addSubview(slogonView)
        //设置约束
        slogonView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(100)
        }
        
        addComposeButtons()
    }

    
    //MARK: -
    //MARK: - 点击屏幕移除当前视图
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
//self.removeFromSuperview()
        for (index,btn) in buttonArray.reversed().enumerated() {
            self.startAnimation(btn: btn, index: index,isShow: false)
        }

    }
//MARK: -
//MARK: - 添加6个按钮
private func addComposeButtons(){
    
//    let btnW: CGFloat = 80
//    let btnH: CGFloat = 110
    let btnMargin = (ScreenWidth - 3 * composeBtnImageWidth) / 4
    
    for i in 0..<6 {
        let btn = SinaComposeButton()
        //应该确定对应的列的索引
        let coloumIndex = i % 3//列
        let rowIndex = i / 3//行
//
//        let btnX = btnMargin + (btnW + btnMargin) * CGFloat(coloumIndex)
//        let btnY = (btnH + btnMargin) * CGFloat(rowIndex)
//        btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
//        btn.backgroundColor = UIColor.red
        ///////////////////
        //计算 x
        let btnX = btnMargin + (composeBtnImageWidth + btnMargin) * CGFloat(coloumIndex)
        //计算 y
        let btnY = (composeBtnHeight + btnMargin) * CGFloat(rowIndex) + ScreenHeight
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_friend"), for: .normal)
        btn.setTitle("@好友", for: .normal)
        btn.frame = CGRect(x: btnX, y: btnY, width: composeBtnImageWidth, height: composeBtnHeight)

        self.addSubview(btn)
        buttonArray.append(btn)
        
        //监听按钮的点击事件
        btn.addTarget(self, action: #selector(composeBtnDidClick(btn:)), for: .touchUpInside)

        
    }

}
    //MARK: -
    //MARK: - 将要添加至父视图时开始动画
    override func willMove(toSuperview newSuperview: UIView?) {
        
        super.willMove(toSuperview: newSuperview)
        //开始动画
        for (index,btn) in buttonArray.enumerated() {
            
            self.startAnimation(btn: btn, index: index)
        }
        //动画执行完毕之后,移除视图(延时移除)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0, execute: {
            self.removeFromSuperview()
        })
//
    }
    //MARK: -
    //MARK: - 按钮的动画效果
    private func startAnimation(btn: SinaComposeButton,index: Int, isShow: Bool = true) {
        /*
         POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
         
         CGPoint point = [tap locationInView:self.view];
         
         springAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, point.x, point.y)];
         
         
         //弹性值
         springAnimation.springBounciness = 20.0;
         //弹性速度
         springAnimation.springSpeed = 20.0;
         
         [_springView pop_addAnimation:springAnimation forKey:@"changeframe"];
         */
        //实例化弹性动画对象
        
        
        //添加动画对象
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        //设置属性
        springAnimation?.springBounciness = 10.0
        springAnimation?.springSpeed = 10.0
        //设置延迟时间
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.25
        let btnCenterY = btn.center.y + CGFloat(isShow ? -350 : 350)
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
        btn.pop_add(springAnimation, forKey: nil)
        
    }
    //MARK: -
    //MARK: - 按钮点击事件
    @objc private func composeBtnDidClick(btn: SinaComposeButton) {
        //1.被点击的按钮执行放大 其他按钮执行缩小   -> 修改transform
        UIView.animate(withDuration: 0.5, animations: {
            //遍历数组 获取按钮
            for composeBtn in self.buttonArray {
                if btn == composeBtn {
                    //就是被点击的按钮 放大\
                    composeBtn.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                } else {
                    //其他按钮 缩小
                    composeBtn.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                    //修改alpha
                    composeBtn.alpha = 0.1
                }
                
                
            }
            }, completion: { (_) -> () in
                //2.跳转对应的控制器
                //通过网络数据已经解析了一个控制器名称的字符串
                let className = "mySina.SinaComposeVCViewController"
                //1. 获取类型
                guard let classType = NSClassFromString(className) as? UIViewController.Type else {
                    return
                }
                
                //2.根据类型实例化控制器对象
                let vc = classType.init()
//                print(vc)
                let nav = UINavigationController(rootViewController: vc)
                UIApplication.shared.keyWindow?.rootViewController?.present(nav, animated: true, completion: {
                
                    self.removeFromSuperview()
                })
                
                
        })
        
        
    }

    

}
